/**
 * @Title: HomeActivity.java
 * @Package com.sinoglobal.waitingMe.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-8-30 上午11:55:09
 */

package com.sinoglobal.wallet.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.adapter.W_WithdrawalRecordAdapter;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.entity.W_WithdrawalMoneyEntity;
import com.sinoglobal.wallet.entity.W_CouponListEntity.CouponList;
import com.sinoglobal.wallet.entity.W_WithdrawalMoneyEntity.RsEntity;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.LogUtils;
import com.sinoglobal.wallet.util.NetWorkUtil;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView.OnFooterRefreshListener;
import com.sinoglobal.wallet.view.pulltorefresh.WPullToRefreshView.OnHeaderRefreshListener;

/**
 * @author clf
 * @createdate 2015-10-22
 * @Description: TODO(提现记录)
 */
public class W_WithDrawalRecordActivity extends W_SinoBaseActivity implements OnFooterRefreshListener, OnHeaderRefreshListener { 
	private WPullToRefreshView withdrawal_record_pulltorefresh;
	private ListView withdrawal_record_listview;
	private W_WithdrawalRecordAdapter adapter;
    private int page=1;
    private String count="20";
	private List<RsEntity> list=new ArrayList<RsEntity>();
	private LinearLayout withdrawal_record_noempty_layout;
	private boolean isShowProgressDialog=true;

	private int totalpages;//总页数
	private TextView withdrawal_record_noempty_txt;
	private String instrution="";
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.wallet_withdrawal_record_layout);
		mTemplateTitleText.setText("提现记录");
		instrution = getIntent().getStringExtra("instrution");
		
		initView();
		getData() ;
	}
	private void initView() {
		adapter=new W_WithdrawalRecordAdapter(this);
		withdrawal_record_pulltorefresh = (WPullToRefreshView) findViewById(R.id.withdrawal_record_pulltorefresh);
		withdrawal_record_listview = (ListView) findViewById(R.id.withdrawal_record_listview);
		withdrawal_record_noempty_layout = (LinearLayout) findViewById(R.id.withdrawal_record_noempty_layout);
		withdrawal_record_noempty_txt = (TextView) findViewById(R.id.withdrawal_record_noempty_txt);
		withdrawal_record_pulltorefresh.setOnFooterRefreshListener(this);
		withdrawal_record_pulltorefresh.setOnHeaderRefreshListener(this); 
		withdrawal_record_listview.setAdapter(adapter);
		withdrawal_record_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				RsEntity rsEntity = list.get(position);
                Bundle bundle = new Bundle();
                if(!instrution.equals("")){
                    bundle.putString("Instrution",instrution);}
                bundle.putString("Fees",rsEntity.getFees());
                bundle.putString("State",rsEntity.getStatus());
                bundle.putString("BankName",rsEntity.getBankName() + "("
        				+ rsEntity.getCardSn().substring(rsEntity.getCardSn().toCharArray().length-4, rsEntity.getCardSn().toCharArray().length) + ")");
                bundle.putString("Money",rsEntity.getAmount());
                LogUtils.v("Money==="+rsEntity.getAmount()+"BankName===="+rsEntity.getBankName() + "("
        				+ rsEntity.getCardSn().substring(rsEntity.getCardSn().toCharArray().length-4, rsEntity.getCardSn().toCharArray().length) + ")"+"Fees===="+rsEntity.getFees()+"State==="+rsEntity.getStatus());
                goIntent(W_WithDrawalRecordDetailActivity.class,bundle);
			}
		});
		
	}
	@Override
	public void onHeaderRefresh(WPullToRefreshView view) {
		// TODO Auto-generated method stub
		withdrawal_record_pulltorefresh.setEnablePullLoadMoreDataStatus(true);
		isShowProgressDialog=false;
		page=1;
		getData();
	}
	@Override
	public void onFooterRefresh(WPullToRefreshView view) {
		// TODO Auto-generated method stub
		isShowProgressDialog=false;
		if (page <=totalpages) {
			getData();
		
		} else {
			Toast.makeText(W_WithDrawalRecordActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
			withdrawal_record_pulltorefresh.setEnablePullLoadMoreDataStatus(false);
			withdrawal_record_pulltorefresh.onFooterRefreshComplete();
		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		 if(id==R.id.wallet_noData){
			 if(NetWorkUtil.getNetWorkInfoType(getApplicationContext())==NetWorkUtil.NONE){
					showToast("请检查网络是否连接！");	
					 loadError(true);
					}else{
						loadError(false);;
						getData();
					}
		}
		
	}
	private void getData() {
		MyAsyncTask<W_WithdrawalMoneyEntity> task = new MyAsyncTask<W_WithdrawalMoneyEntity>(true,this,true) {
			@Override
			public W_WithdrawalMoneyEntity before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getDrawMoneyLog(W_WithDrawalRecordActivity.this, page+"", count);
			}

			@Override
			public void after(W_WithdrawalMoneyEntity result) {
				if (result != null) {
					if (result.getCode().equals("100")) {
						
						  loadError(false);
							if(result.getRs().size()==0){
								withdrawal_record_noempty_layout.setVisibility(View.VISIBLE);
								return;
							} 
							withdrawal_record_noempty_layout.setVisibility(View.GONE);
							if (page == 1) {
								list=result.getRs();
								LocalClearList(list);
								  adapter.setListData(list);
			                      
			                    } else {
			                    	LocalClearList(result.getRs());
			                    	list.addAll(result.getRs());
			                    }
						
									adapter.setListData(list);
								
								
									   totalpages = result.getNum();
								
								page++;
								withdrawal_record_pulltorefresh.onFooterRefreshComplete();
								withdrawal_record_pulltorefresh.onHeaderRefreshComplete();
					}else{
						 withdrawal_record_pulltorefresh.onFooterRefreshComplete();
						 withdrawal_record_pulltorefresh.onHeaderRefreshComplete();
						Toast.makeText(W_WithDrawalRecordActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					 withdrawal_record_pulltorefresh.onFooterRefreshComplete();
					 withdrawal_record_pulltorefresh.onHeaderRefreshComplete();
					Toast.makeText(W_WithDrawalRecordActivity.this, result.getMsg(), Toast.LENGTH_SHORT).show();
				}
				
			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	}
	private void LocalClearList(List<RsEntity> withlist) {

		List<RsEntity> list = new ArrayList<RsEntity>();
		for (RsEntity list1 : withlist) {
			if (list1.getStatus().equals("4")) {
				list.add(list1);
			}
		}
		for (int i = 0; i < list.size(); i++) {
			withlist.remove(list.get(i));
		}
	}
}
